import { queryAll, remove, search, create, update } from '../services/billUphold'
import { message } from 'antd'

export default {

  namespace: 'billUphold',

  state: {
    load : true,
    list: [],
    currentItem: {},
    modalVisible: false,
    modalType: 'create',
    selectedRowKeys:[],
    selectedRows:[],
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        const pathname = location.pathname
        if(pathname === '/billUphold'){
          dispatch({
            type: 'query',
            payload: location.query,
          })
        }
      })
    },
  },

  effects: {

    *fetch({ payload }, { call, put }) {  // eslint-disable-line
      yield put({ type: 'save' });
    },

    *create({ payload }, { call, put }){
      // const item = payload.billItem
      const data = yield call(create, payload)

      if(data){
        yield put ({type: 'hideLoading'})
        yield put ({type: 'query'})
        message.success('新增成功！',1)
      }
    },

    *query({ payload }, { call, put }){
      const data = yield call(queryAll)
      yield put ({type: 'hideLoading'})

      if(data){
        yield put({
          type: 'querySuccess',
          payload: {
            list: data,
          },
        })
      }
    },

    *update ({ payload }, { select, call, put }) {
      const id = yield select(({ billUphold }) => billUphold.currentItem.id)
      const newBill = { ...payload, id }
      const data = yield call(update, newBill)
      if (data) {
        yield put({ type: 'hideModal' })
        yield put({ type: 'query' })
        message.success('修改成功！',1)
      } else {
        throw data
      }
    },

    *'delete' ({ payload }, { call, put }) {
      const data = yield call(remove, { payload })
      if(data){
        yield put({ type: 'query' })
        message.success('删除成功！',1)
      }
    },

    *search({ payload }, {call, put }){
      const { payerId, billName, customerName, customerNum, sex } =payload;
      const list = yield call(queryAll);
      // const data = yield call(search, {payload})
      let data = list;

      if(payerId){
        data = data.filter((item) => item.payerId === payerId);
      }
      if(billName){
        data = data.filter((item) => item.billName && item.billName.match(billName));
      }
      if(customerName){
        data = data.filter((item) => item.customerName && item.customerName.match(customerName));
      }
      if(customerNum){
        data = data.filter((item) => item.customerNum === customerNum);
      }
      if(sex){
        data = data.filter((item) => item.sex === sex);
      }

      if(data){
        yield put({
          type: 'querySuccess',
          payload: {
            list: data,
          },
        })
      }
    }
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },

    querySuccess (state, action) {
      const { list } = action.payload
      return { ...state,
        list,
       }
    },

    showModal (state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },

    hideModal (state) {
      return { ...state, modalVisible: false }
    },

    //隐藏加载
    hideLoading (state,action) {
      return {
        load: false
      }
    },

  },
};
